home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEVEPSN.C < prev    next >
C/C++ Source or Header  |  1993-05-24  |  14KB  |  454 lines

  1. /* Copyright (C) 1989-1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevepsn.c */
  20. /*
  21.  * Epson (and similar) dot-matrix printer driver for Ghostscript.
  22.  *
  23.  * Three devices are defined here: 'epson', 'eps9high', and 'ibmpro'.
  24.  * The 'epson' device is the generic device, for 9-pin and 24-pin printers.
  25.  * 'eps9high' is a special mode for 9-pin printers where scan lines are
  26.  * interleaved in multiple passes to produce high vertical resolution at
  27.  * the expense of several passes of the print head.  'ibmpro' is for the
  28.  * IBM ProPrinter, which has slightly (but only slightly) different
  29.  * control codes.
  30.  *
  31.  * Thanks to David Wexelblat (dwex@mtgzfs3.att.com) for the 'eps9high' code,
  32.  * and to James W. Birdsall (jwbirdsa@picarefy.picarefy.com) for the
  33.  * 'ibmpro' modifications.
  34.  */
  35. #include "gdevprn.h"
  36.  
  37. /*
  38.  * Define whether the printer is archaic -- so old that it doesn't
  39.  * support settable tabs, pitch, or left margin.  (This should be a
  40.  * run-time property....)  Note: the IBM ProPrinter is archaic.
  41.  */
  42. /*#define ARCHAIC 1*/
  43.  
  44. /*
  45.  * Define the minimum distance for which it's worth converting white space
  46.  * into a tab.  This can be specified in pixels (to save transmission time),
  47.  * in tenths of an inch (for printers where tabs provoke actual head motion),
  48.  * or both.  The distance must meet BOTH criteria for the driver to tab,
  49.  * so an irrelevant criterion should be set to 0 rather than infinite.
  50.  */
  51. #define MIN_TAB_PIXELS 10
  52. #define MIN_TAB_10THS 15
  53.  
  54. /*
  55.  * Valid values for X_DPI:
  56.  *
  57.  *    For 9-pin printers: 60, 120, 240
  58.  *    For 24-pin printers: 60, 120, 180, 240, 360
  59.  *
  60.  * The value specified at compile time is the default value used if the
  61.  * user does not specify a resolution at runtime.
  62.  */
  63. #ifndef X_DPI
  64. #  define X_DPI 240
  65. #endif
  66.  
  67. /*
  68.  * For Y_DPI, a given printer will support a base resolution of 60 or 72;
  69.  * check the printer manual.  The Y_DPI value must be a multiple of this
  70.  * base resolution.  Valid values for Y_DPI:
  71.  *
  72.  *    For 9-pin printers: 1*base_res
  73.  *    For 24-pin printers: 1*base_res, 3*base_res
  74.  *
  75.  * The value specified at compile time is the default value used if the
  76.  * user does not specify a resolution at runtime.
  77.  */
  78.  
  79. #ifndef Y_BASERES
  80. #  define Y_BASERES 72
  81. #endif
  82. #ifndef Y_DPI
  83. #  define Y_DPI (1*Y_BASERES)
  84. #endif
  85.  
  86. /* The device descriptors */
  87. private dev_proc_get_initial_matrix(eps_get_initial_matrix);
  88. private dev_proc_print_page(eps_print_page1);
  89. private dev_proc_print_page(eps_print_page2);
  90. private dev_proc_print_page(ibmpro_print_page);
  91.  
  92. gx_device_procs prn_eps_procs =
  93.   prn_matrix_procs(gdev_prn_open, eps_get_initial_matrix,
  94.     gdev_prn_output_page, gdev_prn_close);
  95.  
  96. /* Standard Epson device */
  97. gx_device_printer far_data gs_epson_device =
  98.   prn_device(prn_eps_procs, "epson",
  99.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  100.     X_DPI, Y_DPI,
  101.     0.2, 0.95, 0, 1.0,            /* margins */
  102.     1, eps_print_page1);
  103.  
  104. /* High-res (interleaved) 9-pin device */
  105. gx_device_printer far_data gs_eps9high_device = 
  106.   prn_device(prn_eps_procs, "eps9high",
  107.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  108.     X_DPI, 3*Y_BASERES,
  109.     0.2, 0.95, 0, 1.0,            /* margins */
  110.     1, eps_print_page2);
  111.  
  112. /* IBM ProPrinter device */
  113. gx_device_printer far_data gs_ibmpro_device =
  114.   prn_device(prn_eps_procs, "ibmpro",
  115.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  116.     X_DPI, Y_DPI,
  117.     0.2, 0.95, 0, 1.0,            /* margins */
  118.     1, ibmpro_print_page);
  119.  
  120. /* ------ Driver procedures ------ */
  121.  
  122. /* Shift the origin from the first printable pixel (as defined by the */
  123. /* top and left margins), to the top left corner of the physical page. */
  124. private void
  125. eps_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  126. {    gx_default_get_initial_matrix(dev, pmat);
  127.     pmat->tx -= dev->l_margin * dev->x_pixels_per_inch;
  128.     pmat->ty -= dev->t_margin * dev->y_pixels_per_inch;
  129. }
  130.  
  131. /* ------ Internal routines ------ */
  132.  
  133. /* Forward references */
  134. private void eps_output_run(P6(byte *, int, int, char, FILE *, int));
  135.  
  136. /* Send the page to the printer. */
  137. #define DD 0x80                /* double density flag */
  138. private int
  139. eps_print_page(gx_device_printer *pdev, FILE *prn_stream, int y_9pin_high,
  140.   const char *init_string, int init_length, const char *end_string,
  141.   int archaic)
  142. {    
  143.     static const char graphics_modes_9[5] =
  144.     {    
  145.     -1, 0 /*60*/, 1 /*120*/, -1, DD+3 /*240*/
  146.     };
  147.  
  148.     static const char graphics_modes_24[7] =
  149.     {    
  150.         -1, 32 /*60*/, 33 /*120*/, 39 /*180*/,
  151.     -1, -1, DD+40 /*360*/
  152.     };
  153.  
  154.     int y_24pin = (y_9pin_high ? 0 : pdev->y_pixels_per_inch > 72);
  155.     int in_y_mult = ((y_24pin | y_9pin_high) ? 3 : 1);
  156.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  157.     /* Note that in_size is a multiple of 8. */
  158.     int in_size = line_size * (8 * in_y_mult);
  159.     byte *buf1 = (byte *)gs_malloc(in_size, 1, "eps_print_page(buf1)");
  160.     byte *buf2 = (byte *)gs_malloc(in_size, 1, "eps_print_page(buf2)");
  161.     byte *in = buf1;
  162.     byte *out = buf2;
  163.     int out_y_mult = (y_24pin ? 3 : 1);
  164.     int x_dpi = pdev->x_pixels_per_inch;
  165.     char start_graphics =
  166.         (y_24pin ? graphics_modes_24 : graphics_modes_9)[x_dpi / 60];
  167.     int first_pass = (start_graphics & DD ? 1 : 0);
  168.     int last_pass = first_pass * 2;
  169.     int y_passes = (y_9pin_high ? 3 : 1);
  170.     int dots_per_space = x_dpi / 10;    /* pica space = 1/10" */
  171.     int bytes_per_space = dots_per_space * out_y_mult;
  172.     int tab_min_pixels = x_dpi * MIN_TAB_10THS / 10;
  173.     int skip = 0, lnum = 0, pass, ypass;
  174.  
  175.     /* Check allocations */
  176.     if ( buf1 == 0 || buf2 == 0 )
  177.     {    if ( buf1 ) 
  178.           gs_free((char *)buf1, in_size, 1, "eps_print_page(buf1)");
  179.         if ( buf2 ) 
  180.           gs_free((char *)buf2, in_size, 1, "eps_print_page(buf2)");
  181.         return_error(gs_error_VMerror);
  182.     }
  183.  
  184.     /* Initialize the printer and reset the margins. */
  185.     fwrite(init_string, 1, init_length, prn_stream);
  186.     if ( init_string[init_length - 1] == 'Q' )
  187.         fputc((int)(pdev->width / pdev->x_pixels_per_inch * 10) + 2,
  188.               prn_stream);
  189.  
  190.     /* Calculate the minimum tab distance. */
  191.     if ( tab_min_pixels < max(MIN_TAB_PIXELS, 3) )
  192.         tab_min_pixels = max(MIN_TAB_PIXELS, 3);
  193.     tab_min_pixels -= tab_min_pixels % 3;    /* simplify life */
  194.  
  195.     /* Print lines of graphics */
  196.     while ( lnum < pdev->height )
  197.     {    
  198.         byte *in_data;
  199.         byte *inp;
  200.         byte *in_end;
  201.         byte *out_end;
  202.         byte *out_blk;
  203.         register byte *outp;
  204.         int lcnt;
  205.  
  206.         /* Copy 1 scan line and test for all zero. */
  207.         gdev_prn_get_bits(pdev, lnum, in, &in_data);
  208.         if ( in_data[0] == 0 &&
  209.              !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
  210.            )
  211.             {    
  212.             lnum++;
  213.             skip += 3 / in_y_mult;
  214.             continue;
  215.         }
  216.  
  217.         /* Vertical tab to the appropriate position. */
  218.         while ( skip > 255 )
  219.         {    
  220.             fputs("\033J\377", prn_stream);
  221.             skip -= 255;
  222.         }
  223.         if ( skip )
  224.         {
  225.             fprintf(prn_stream, "\033J%c", skip);
  226.         }
  227.  
  228.         /* Copy the the scan lines. */
  229.             lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
  230.         if ( lcnt < 8 * in_y_mult )
  231.         {    /* Pad with lines of zeros. */
  232.             memset(in + lcnt * line_size, 0,
  233.                    in_size - lcnt * line_size);
  234.         }
  235.  
  236.         if ( y_9pin_high )
  237.         {    /* Shuffle the scan lines */
  238.             byte *p;
  239.             int i;
  240.             static const char index[] =
  241.             {  0,  8, 16,  1,  9, 17,  
  242.                2, 10, 18,  3, 11, 19,
  243.                4, 12, 20,  5, 13, 21,
  244.                6, 14, 22,  7, 15, 23
  245.             };
  246.  
  247.             for ( i = 0; i < 24; i++ )
  248.             {
  249.                 memcpy(out+(index[i]*line_size),
  250.                        in+(i*line_size), line_size);
  251.             }
  252.             p = in;
  253.             in = out;
  254.             out = p;
  255.         }
  256.  
  257.     for ( ypass = 0; ypass < y_passes; ypass++ )
  258.     {
  259.         for ( pass = first_pass; pass <= last_pass; pass++ )
  260.         {
  261.         /* We have to 'transpose' blocks of 8 pixels x 8 lines, */
  262.         /* because that's how the printer wants the data. */
  263.         /* If we are in a 24-pin mode, we have to transpose */
  264.         /* groups of 3 lines at a time. */
  265.  
  266.             if ( pass == first_pass )
  267.             {
  268.             out_end = out;
  269.             inp = in;
  270.             in_end = inp + line_size;
  271.     
  272.             if ( y_24pin )
  273.                     { 
  274.                         for ( ; inp < in_end; inp++, out_end += 24 )
  275.                         { 
  276.                             gdev_prn_transpose_8x8(inp, line_size, out_end, 3);
  277.                             gdev_prn_transpose_8x8(inp + line_size * 8, 
  278.                                line_size, out_end + 1, 3);
  279.                             gdev_prn_transpose_8x8(inp + line_size * 16, 
  280.                                line_size, out_end + 2, 3);
  281.                 }
  282.                 /* Remove trailing 0s. */
  283.                 while ( out_end > out && out_end[-1] == 0 &&
  284.                                out_end[-2] == 0 && out_end[-3] == 0)
  285.                     {
  286.                      out_end -= 3;
  287.                 }
  288.                     }
  289.             else
  290.                     { 
  291.                         for ( ; inp < in_end; inp++, out_end += 8 )
  292.                         { 
  293.                         gdev_prn_transpose_8x8(inp + (ypass * 8*line_size), 
  294.                                line_size, out_end, 1);
  295.                 }
  296.             /* Remove trailing 0s. */
  297.             while ( out_end > out && out_end[-1] == 0 )
  298.                 {
  299.                        out_end--;
  300.             }
  301.                     }
  302.         }
  303.  
  304.         for ( out_blk = outp = out; outp < out_end; )
  305.                 { 
  306.                  /* Skip a run of leading 0s.  At least */
  307.                     /* tab_min_pixels are needed to make tabbing */
  308.                 /* worth it.  We do everything by 3's to */
  309.                 /* avoid having to make different cases */
  310.                 /* for 9- and 24-pin. */
  311.            if ( !archaic &&
  312.             *outp == 0 && out_end - outp >= tab_min_pixels &&
  313.             (outp[1] | outp[2]) == 0 &&
  314.             !memcmp((char *)outp, (char *)outp + 3,
  315.                 tab_min_pixels - 3)
  316.               )
  317.                     {
  318.                     byte *zp = outp;
  319.             int tpos;
  320.             byte *newp;
  321.            
  322.             outp += tab_min_pixels;
  323.                     while ( outp + 3 <= out_end && 
  324.                              *outp == 0 &&
  325.                         outp[1] == 0 && outp[2] == 0 )
  326.                     {
  327.                 outp += 3;
  328.                     }
  329.             tpos = (outp - out) / bytes_per_space;
  330.             newp = out + tpos * bytes_per_space;
  331.             if ( newp > zp + 10 )
  332.                     { 
  333.                     /* Output preceding bit data.*/
  334.                        if ( zp > out_blk )    
  335.                     {
  336.                         /* only false at beginning of line */
  337.                      eps_output_run(out_blk, (int)(zp - out_blk),
  338.                                    out_y_mult, start_graphics, 
  339.                            prn_stream, pass);
  340.                 }
  341.                 /* Tab over to the appropriate position. */
  342.                 fprintf(prn_stream, "\033D%c%c\t", tpos, 0);
  343.                 out_blk = outp = newp;
  344.             }
  345.             }
  346.             else
  347.             {
  348.                     outp += out_y_mult;
  349.             }
  350.                 }
  351.         if ( outp > out_blk )
  352.             {
  353.             eps_output_run(out_blk, (int)(outp - out_blk),
  354.                        out_y_mult, start_graphics,
  355.                    prn_stream, pass);
  356.             }
  357.  
  358.         fputc('\r', prn_stream);
  359.         }
  360.         if ( ypass < y_passes - 1 )
  361.         fputs("\033J\001", prn_stream);
  362.     }
  363.     skip = 24 - y_passes + 1;        /* no skip on last Y pass */
  364.     lnum += 8 * in_y_mult;
  365.     }
  366.  
  367.     /* Eject the page and reinitialize the printer */
  368.     fputs("\f\033@", prn_stream);
  369.     fflush(prn_stream);
  370.  
  371.     gs_free((char *)buf2, in_size, 1, "eps_print_page(buf2)");
  372.     gs_free((char *)buf1, in_size, 1, "eps_print_page(buf1)");
  373.     return 0;
  374. }
  375.  
  376. /* Output a single graphics command. */
  377. /* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
  378. private void
  379. eps_output_run(byte *data, int count, int y_mult,
  380.   char start_graphics, FILE *prn_stream, int pass)
  381. {    
  382.     int xcount = count / y_mult;
  383.  
  384.     fputc(033, prn_stream);
  385.     if ( !(start_graphics & ~3) )
  386.     {    
  387.         fputc("KLYZ"[start_graphics], prn_stream);
  388.     }
  389.     else
  390.     {    
  391.         fputc('*', prn_stream);
  392.         fputc(start_graphics & ~DD, prn_stream);
  393.     }
  394.     fputc(xcount & 0xff, prn_stream);
  395.     fputc(xcount >> 8, prn_stream);
  396.     if ( !pass )
  397.     {
  398.         fwrite(data, 1, count, prn_stream);
  399.     }
  400.     else
  401.     {    
  402.         /* Only write every other column of y_mult bytes. */
  403.         int which = pass;
  404.         register byte *dp = data;
  405.         register int i, j;
  406.  
  407.         for ( i = 0; i < xcount; i++, which++ )
  408.         {
  409.             for ( j = 0; j < y_mult; j++, dp++ )
  410.             {
  411.                 putc(((which & 1) ? *dp : 0), prn_stream);
  412.             }
  413.         }
  414.     }
  415. }
  416.  
  417. /* The print_page procedures are here, to avoid a forward reference. */
  418. #ifndef ARCHAIC
  419. #  define ARCHAIC 0
  420. #endif
  421.  
  422. #define ESC 0x1b
  423. private const char eps_init_string[] = {
  424. #if ARCHAIC
  425.     ESC, '@', 'r', ESC, 'Q'
  426. #else
  427.     ESC, '@', ESC, 'P', ESC, 'l', 0, '\r', ESC, 'Q'
  428. #endif
  429. };
  430.  
  431. private int
  432. eps_print_page1(gx_device_printer *pdev, FILE *prn_stream)
  433. {
  434.     return eps_print_page(pdev, prn_stream, 0, eps_init_string,
  435.                   sizeof(eps_init_string), "\f\033@", ARCHAIC);
  436. }
  437.  
  438. private int
  439. eps_print_page2(gx_device_printer *pdev, FILE *prn_stream)
  440. {
  441.     return eps_print_page(pdev, prn_stream, 1, eps_init_string,
  442.                   sizeof(eps_init_string), "\f\033@", ARCHAIC);
  443. }
  444.  
  445. private int
  446. ibmpro_print_page(gx_device_printer *pdev, FILE *prn_stream)
  447. {
  448.     static const char ibmpro_init_string[] = {
  449.         ESC, '3', 0x30
  450.     };
  451.     return eps_print_page(pdev, prn_stream, 0, ibmpro_init_string,
  452.                   sizeof(ibmpro_init_string), "\f", 1);
  453. }
  454.